package com.goods.service;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.goods.dao.ProductMapper;
import com.goods.pojo.Product;
import com.goods.tool.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class ProductService {
    @Autowired
    private ProductMapper mapper;
    public Result getProducts(Integer page , Integer pageSize){
        PageHelper.startPage(page,pageSize);
        List<Product> products = mapper.getProducts();
        if(products.isEmpty())
            return Result.success();
        Page<Product> p =  (Page<Product>) products;
        return Result.success(p.getResult().toArray(),(int) p.getTotal());
    }

    public Result addProduct(Product product){
        int i = mapper.addProduct(product);
        if(i != 1){
            return Result.error("添加失败");
        }
        return Result.success();
    }

    public Result deleteProduct(Integer id){
        int i = mapper.deleteProduct(id);
        if(i != 1){
            return Result.error("删除失败");
        }
        return Result.success();
    }
    public Result updateProduct(Product product){
        int i = mapper.updateProduct(product);
        if(i != 1){
            return Result.error("更新失败");
        }
        return Result.success();
    }
}
